/**
 * Created by DevilsEyes on 2016/4/19.
 */
'use strict';
var md5 = require('blueimp-md5');
var $user = require('../../db/user.js');

module.exports = {

    sync_filter: (user)=> {
        return {
            _id: user._id,
            id: user.id,
            name: user.name,
            class: user.class,
            nickname: user.nickname,
            email: user.email
        }
    },

    getAllClass: ()=> {
        return new Promise((O, X)=> {

            $user
                .find({})
                .distinct('class')
                .then((list)=> {
                    list = list.filter((v)=> {
                        return v.length <= 8;
                    });
                    O(list);
                })
        })
    },

    getUsersByClass: (Class)=> {
        //Class格式: A#B#C

        return new Promise((O, X)=> {
            try {
                let Reg_str = `(^|#)(${Class.replace('#', '|')})($|#)`;
                var Reg_search = new RegExp(Reg_str);

                $user
                    .where('class', Reg_search)
                    .where('role', 1)
                    .then((students)=> {
                        O(students);
                    })
                    .catch((e)=> {
                        console.log(e);
                        X({
                            code: 9999,
                            msg: '查询时发生错误'
                        })
                    })

            } catch (e) {
                console.log(e);
                X({
                    code: 9999,
                    msg: '查询条件发生错误'
                })
            }

        })
    },

    verify: (id, pass)=> {


        var user = null;

        return new Promise((O, X)=> {
            if (pass === true) {
                $user
                    .find({id: id, role: -2})
                    .then((users)=> {
                        if (users[0]) {
                            user = users[0];

                            $user
                                .find({id: id, role: -2})
                                .update({
                                    role: -1
                                })
                                .then(()=> {

                                    O(user);

                                })

                        } else {
                            X({
                                code: 9999,
                                msg: '没有该学生'
                            })
                        }
                    })

            } else if (pass === false) {
                $user
                    .find({id: id, role: -2})
                    .remove({id: id, role: -2})
                    .then(()=> {
                        O();
                    })
            }
        })
    },

    getVerifyList: (index, limit)=> {

        return new Promise((O, X)=> {

            $user
                .find({role: -2})
                .then((users)=> {
                    O(users);
                })
                .catch((e)=> {
                    X({
                        code: 9999,
                        msg: '数据查询出错:' + e.toString && e.toString()
                    })
                });
        })

    },

    getUserList: (index, limit)=> {

        return new Promise((O, X)=> {

            $user
                .find({role: 1})
                .then((users)=> {
                    O(users);
                })
                .catch((e)=> {
                    X({
                        code: 9999,
                        msg: '数据查询出错:' + e.toString && e.toString()
                    })
                });
        })

    },

    searchByMd5Id: (md5_id)=> {

        return new Promise((O, X)=> {
            $user
                .find({secId: md5_id})
                .then((users) => {
                    users = users || [];
                    if (users.length > 0) {
                        O(users[0]);
                    }
                    else {
                        X({
                            code: 9999,
                            msg: '错误的 SecId'
                        })
                    }
                });
        })
    },


    // getAll: () => {
    //     return $user
    //         .find({});
    //     // .sort({status: 'desc'});
    // },
    //
    // getSome: (limit) => {
    //     return $user
    //         .find({status: 0})
    //         .sort({deep: 'desc', id: 'asc'})
    //         .limit(limit)
    // },

    //危险方法
    force_creat: (obj) => {

        var user_Entity = new $user({
            id: obj.id,//学号
            password: obj.password,//密码
            secId: md5(obj.id),//md5学号
            secPassword: md5(obj.password),//md5密码
            role: obj.role || -2,//权限

            class: obj.class,//班级
            name: obj.name,//姓名
            email: obj.email,//邮箱
            nickname: obj.nickname//昵称
        });
        return new Promise((O, X)=> {
            user_Entity.save(()=> {
                O();
            })
        })
    },
    clear: () => {
        return new Promise((O, X)=> {
            $user
                .remove({})
                .then(()=> {
                    O()
                })
        })
    },
    update: (_id, cp) => {
        return new Promise((O, X)=> {
            $user
                .find({_id: _id})
                .update(cp)
                .then(()=> {
                    O();
                })
        })
    },
    getAll: () => {
        return new Promise((O, X)=> {
            $user
                .find({})
                .then((users)=> {
                    O(users);
                })
        })
    }

};